package com.nus.database;

import com.nus.beans.User;
import com.nus.services.ConversionService;
import com.nus.services.EncodingService;
import oracle.jdbc.pool.OracleDataSource;
import oracle.jdbc.xa.client.OracleXADataSource;
import org.apache.log4j.Logger;

import javax.sql.DataSource;
import java.sql.*;

/**
 * Created by IntelliJ IDEA.
 * User: dell
 * Date: 9/15/12
 * Time: 10:08 PM
 * To change this template use File | Settings | File Templates.
 */
public abstract class DBManager {
    protected static Logger logger = Logger.getLogger(DBManager.class.getName());
    protected Connection connection;

    protected DBManager(Connection connection) {
        this.connection = connection;
    }

   /* protected void close(Connection connection, Statement statement, ResultSet resultSet) {
        close(statement, resultSet);
        close(connection);
    }*/

    protected static void close(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                logger.error(e.getMessage());
            }
        }
    }

    protected void close(Statement statement, ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                logger.error(e.getMessage(), e);
            }
        }
        close(statement);
    }

    protected void close(Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                logger.error(e.getMessage(), e);
            }
        }
    }

    protected void setAutoCommit(boolean autoCommit) {
        try {
            connection.setAutoCommit(autoCommit);
        } catch (SQLException e) {
            logger.error(e.getMessage(), e);
        }
    }

    protected void commit() {
        try {
            connection.commit();
        } catch (SQLException e) {
            logger.error(e.getMessage(), e);
        }
    }

    protected void rollback() {
        try {
            connection.rollback();
        } catch (SQLException e) {
            logger.error(e.getMessage(), e);
        }
    }

    public static DataSource getUserDataSource(String dbUrl, User user) {
        OracleDataSource newDataSource = null;
        String dbUserName;
        String dbPassword;
        EncodingService encodingService = new EncodingService();


        try {
            newDataSource = new OracleDataSource();
            dbUserName = encodingService.getDBUserName(user);
            dbPassword = encodingService.getDBPassword(user);
            newDataSource.setUser(dbUserName);
            newDataSource.setPassword(dbPassword);
            newDataSource.setURL(dbUrl);
        } catch (SQLException ex) {
            logger.error(ex.getMessage(), ex);
        }
        return newDataSource;
    }

    public static boolean testDataSource(DataSource dataSource) {
        boolean isWorking = false;
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            isWorking = true;
        } catch (SQLException e) {
            logger.error(e.getMessage());
        } finally {
            close(connection);
        }
        return isWorking;
    }

    public void close(){
        close(connection);
    }
}
